package jiubeirobot.com.robotshop.data.net;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import jiubeirobot.com.robotshop.bean.*;

import org.json.JSONException;
import org.json.JSONObject;

import jiubeirobot.com.robotshop.base.BaseBean;
import jiubeirobot.com.robotshop.base.ListBean;
import jiubeirobot.com.robotshop.utils.DataUtil;
import jiubeirobot.com.robotshop.utils.SharedPreferencesUtils;

/**
 * ==========================
 * 创建人：TheJoker丶豪 on
 * 创建时间：2017/8/18.
 * 创建内容：定义请求数据的基类
 * ==========================
 */

public class APIManager {
    private static APIManager apiManager;
    private INetLoad iNetLoad = new NetLoadImpl();
    private Gson gson = new Gson();

    public static APIManager getInstance() {
        if (apiManager == null) {
            synchronized (APIManager.class) {
                if (apiManager == null) {
                    apiManager = new APIManager();
                }
            }
        }
        return apiManager;
    }

    /**
     * 商品列表
     */
    public void shopList(Context context, String class_id, String brand_id, String min_price, String max_price,
                         String goods_name, String begin_time, String end_time, String order_by, String order_type,int pageNumber,int pageSize) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("class_id", class_id);
            jsonObject.put("brand_id", brand_id);
            jsonObject.put("min_price", min_price);
            jsonObject.put("max_price", max_price);
            jsonObject.put("goods_name", goods_name);
            jsonObject.put("begin_time", begin_time);
            jsonObject.put("end_time", end_time);
            jsonObject.put("order_by", order_by);
            jsonObject.put("order_type", order_type);
            jsonObject.put("pageNumber",pageNumber);
            jsonObject.put("pageSize",pageSize);
            jsonObject.put("delivery_id", DataUtil.getInstance().getDelivery_Id(context));


//            1.delivery_id				店铺id
//            2.class_id					分类id
//            3.brand_id					品牌id
//            4.min_price					最低价
//            5.max_price				最高价
//            6.goods_name				商品名
//            7.begin_time				上架时间 起始
//            8.end_time					上架时间 终止
//            9.order_by					price-价格/sale-销量
//            10.order_type				asc-升序/其他都

        } catch (JSONException e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(Flags.FLAG_SHOP_LIST, URLS.URL_SHOP_LIST, jsonObject, context, (INetResult) context, new TypeToken<BaseBean<ListBean<ShopListBean>>>() {
        }.getType());

    }

    /**
     * 获取商品详情
     *
     * @param context
     */
    public void shopDetails(Context context, INetResult iNetResult, String goods_id, String delivery_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("goods_id", goods_id);
            jsonObject.put("delivery_id", delivery_id);
            Log.d("print", "请求参数为: " + jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(
                Flags.FLAG_SHOP_DETAILS,
                URLS.URL_SHOP_DETAILS, jsonObject, context, iNetResult, new TypeToken<BaseBean<ObjBean<ShopDetailsBean>>>() {
                }.getType());
    }

    /**
     * 提交机器人信息到服务端
     */
    public void putRobotInfoToServer(Context context, String account, String area_id, String address, String coordinate, String machine_id) {
        JSONObject jsonObject = new JSONObject();
        try {

            jsonObject.put("account", account);
            jsonObject.put("area_id", area_id);
            jsonObject.put("address", address);
            jsonObject.put("coordinate", coordinate);
            jsonObject.put("machine_id", machine_id);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.d("print", "putRobotInfoToServer: " + jsonObject);
        iNetLoad.loadData(Flags.FLAG_PUT_ROBOT_INFO, URLS.URL_PUT_INFO, jsonObject, context, (INetResult) context, new TypeToken<BaseBean<ObjBean<RobotInfoBean>>>() {
        }.getType());
    }

    /**
     * 获取版本号更新版本
     */
    public void getVersionCode(Context context) {
        JSONObject jsonObject = new JSONObject();
        iNetLoad.loadData(Flags.FLAG_VERSION_CODE, URLS.URL_VERSION_CODE, jsonObject, context, (INetResult) context, new TypeToken<BaseBean<ObjBean<VersionCodeBean>>>() {
        }.getType());

    }

    /**
     * 登录管理系统
     */
    public void loginSys(Context context, String username, String password) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", username);
            jsonObject.put("password", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(Flags.FLAG_LOGIN_SYSTEM, URLS.URL_LOGIN_SYSTEM, jsonObject, context, (INetResult) context, new TypeToken<BaseBean<ObjBean<LoginManageBean>>>() {
        }.getType());

    }


    /**
     * 提交提货申请
     * username 当前店主账户
     */
    public void takeGods(Context context, String order_id, String username, String token) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("order_id", order_id);
            jsonObject.put("user_id", username);
            jsonObject.put("token", token);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(Flags.FLAG_TAKE_GODS, URLS.URL_SUER_EXTRACT, jsonObject, context, (INetResult) context, new TypeToken<BaseBean<ObjBean<VersionCodeBean>>>() {
        }.getType());
    }

    /**
     * 关于久碑
     */
    public void getJiuBei(Context context,String id) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("id",id);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        iNetLoad.loadDataForGet(Flags.FLAG_GET_JIUBEI, URLS.URL_GET_JIUBEI, jsonObject, context, (INetResult) context, new TypeToken<BaseBean<ObjBean<VersionCodeBean>>>() {
        }.getType());
    }

    /**
     * 获取当前店主的提货订单信息
     *
     * @param context
     */
    public void getExtractOrder(Context context, String user_id, INetResult iNetResult, String size) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("user_id", user_id);
            jsonObject.put("list_type", 0);
            jsonObject.put("size", size);
            jsonObject.put("begin", 0);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(Flags.FLAG_EXTRACT_ORDER, URLS.EXTRACT_ORDER_INFO, jsonObject, context, iNetResult, new TypeToken<BaseBean<ListBean<VersionCodeBean>>>() {
        }.getType());
    }

    /**
     * 获取当前店主销售订单信息
     *
     * @param context
     */
    public void getSaleOrder(Context context, String user_id, INetResult iNetResult, String size) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("user_id", user_id);
            jsonObject.put("list_type", 1);
            jsonObject.put("size", size);
            jsonObject.put("begin", 0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(Flags.FLAG_SALE_ORDER, URLS.SALE_ORDER_INFO, jsonObject, context, iNetResult, new TypeToken<BaseBean<ListBean<VersionCodeBean>>>() {
        }.getType());
    }


    /***
     * 根据条码获取商品ID
     */
    public void getShopIDByBarcode(Context context, String barcode, String shop_id) {
        JSONObject object = new JSONObject();
        try {
            object.put("bar_code", barcode);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.d("print", "getShopIDByBarcode: " + barcode);
        iNetLoad.loadData(Flags.FLAG_SHOPID_BY_BARCODE,
                URLS.URL_SHOPID_BY_BARCODE, object, context, (INetResult) context,
                new TypeToken<BaseBean<ObjBean<ShopIDByBarcode>>>() {
                }.getType());
    }

    /**
     * 获取个人信息
     */
    public void getAccountInfo(Context context, String delivery_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("delivery_id", delivery_id);
            jsonObject.put("verify", SharedPreferencesUtils.getString(context, "verify"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(Flags.FLAG_GET_ACCOUNT_INFO, URLS.GET_ACCOUNT_INFO, jsonObject, context, (INetResult) context, new TypeToken<BaseBean<ObjBean<AccountInfoBean>>>() {
        }.getType());

    }

    //订单提交
    public void orderSubmit(Context context, INetResult iNetResult, UserSubmitOrderPramsBean pramsBean) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(gson.toJson(pramsBean));
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d("print", "orderSubmit: " + jsonObject);
        iNetLoad.loadData(
                Flags.FLAG_USER_ORDER_SUBMIT,
                URLS.URL_USER_ORDER_SUBMIT, jsonObject, context, iNetResult, new TypeToken<BaseBean<ObjBean<OrderResultInfoBean>>>() {
                }.getType());
    }

    /**
     * 获取支付宝支付
     *
     * @param context
     */
    public void getAliPayInfo(Context context, String user_id, String order_id) {
        JSONObject jsonObject = null;
        try {
            jsonObject.put("user_id", user_id);
            jsonObject.put("order_id", order_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(
                Flags.FLAG_SGOP_PAY_ALI,
                URLS.URL_PAY_ALI, jsonObject, context, (INetResult) context, new TypeToken<BaseBean<ObjBean<OrderResultInfoBean>>>() {
                }.getType());

    }


    /**
     * 获取支微信支付
     *
     * @param context
     */
    public void getWechatPayInfo(Context context, String order_id, String user_id) {
        JSONObject jsonObject = null;
        try {
            jsonObject.put("user_id", user_id);
            jsonObject.put("order_id", order_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(
                Flags.FLAG_SGOP_PAY_WECHAT,
                URLS.URL_PAY_WECHAT, jsonObject, context, (INetResult) context, new TypeToken<BaseBean<ObjBean<OrderResultInfoBean>>>() {
                }.getType());
    }

    /**
     * 获取订单详情
     */
    public void getOrderDtails(Context context, INetResult iNetResult, String user_id, String order_id) {
        JSONObject jsonObject = null;
        try {
            jsonObject.put("user_id", user_id);
            jsonObject.put("order_id", order_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(
                Flags.FLAG_ORDER_DETAILS,
                URLS.URL_ORDER_DETAILS, jsonObject, context, iNetResult, new TypeToken<BaseBean<ObjBean<OrderResultInfoBean>>>() {
                }.getType());

    }

    /***
     * 获取订单状态
     */
    public void getOrderState(Context context, String user_id, String order_id, String token) {
        JSONObject object = new JSONObject();
        try {
            object.put("user_id", user_id);
            object.put("order_id", order_id);
            object.put("token", token);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        iNetLoad.loadData(Flags.FLAG_GET_ORDER_STATE,
                URLS.URL_GET_ORDER_STATUS, object, context, (INetResult) context,
                new TypeToken<BaseBean<ObjBean<OrderStateInfoBean>>>() {
                }.getType());
    }


}
